//
//  ContactsVC.swift
//  com.yunshu.yunshuapp
//
//  Created by 周文杰 on 2017/6/14.
//  Copyright © 2017年 zwj. All rights reserved.
//

import UIKit
import HandyJSON

class ContactsVC: XHListVC {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "厂家通讯录"
        self.navigationItem.titleView = nil
        self.navigationItem.rightBarButtonItem = nil

    }

    override func refresh() {
        
        let params = ["channelname":self.channels,"ext":self.cities,"page":"1"]
        
        _ = NetworkManager.GET(NetworkManager.TONGXUNLU_LIST,params:params) { (status, response) in
            self.tableView.endRefreshing(at: .top)
            
            if(status){
                
                for dic in response.wtmList {
                    let member = JSONDeserializer<Contact>.deserializeFrom(dict:dic as? NSDictionary)
                    self.models.append(member!)
                }
                DispatchQueue.main.async {
                    self.stopLoading()
                    self.tableView.reloadData()
                }            }
        }
    }
    
    override func loadmore() {
        let pageNumStr = String(pageNum)
        let params = ["channelname":self.channels,"ext":self.cities,"page":pageNumStr]

        _ = NetworkManager.GET(NetworkManager.TONGXUNLU_LIST,params:params) { (status, response) in
            self.tableView.endRefreshing(at: .bottom)
            
            for dic in response.wtmList {
                let member = JSONDeserializer<Contact>.deserializeFrom(dict:dic as? NSDictionary)
                self.models.append(member!)
            }
            if response.wtmList.count < 10 {
                self.removeRefreshFooter()
            }
            DispatchQueue.main.async {
                self.stopLoading()
                self.tableView.reloadData()
            }
        }
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let contact: Contact = self.models[indexPath.row] as! Contact
        
        if let cell:ContactCell = tableView.dequeueReusableCell(withIdentifier: "ContactCell") as? ContactCell {
            
            cell.setTitle(contact.name!)
            return cell
        }
        let cell:ContactCell = ContactCell.init(style: .default, reuseIdentifier: "ContactCell")
        cell.setTitle(contact.name!)
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let xianhuo : Contact = self.models[indexPath.row] as! Contact
        let vc = ContactDetailVC.init()
        vc.model = xianhuo
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
